package com.example.demo.serviceimp;

import com.example.demo.entity.Chapter;
import com.example.demo.mapper.ChapterMapper;
import com.example.demo.service.ChapterService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class ChapterServiceImp implements ChapterService {

    @Resource
    private ChapterMapper chapterMapper;

    /**
     * 增加章节
     * */
    @Override
    public int insertchapter(String chapter_name, Integer chapter_type,Integer chapter_node) {
        return chapterMapper.insertchapter(chapter_name,chapter_type,chapter_node);
    }

    @Override
    public List<Chapter> selchapter1() {
        return chapterMapper.selchapter();
    }

    /**
     * 章节查询
     * */
    @Override
    public List<Chapter> selchapter() {
        List<Chapter> list = chapterMapper.selchapter();//查询所有章节
        List<Chapter> newlist = new ArrayList<>();
        for (Chapter c:list){
            if (c.getChapterNode() == 0) {
                Chapter cha = selone(c.getChapterId());
                newlist.add(cha);
            }
        }
        return newlist;
    }
    //子节点
    @Override
    public Chapter selone(Integer chapter_id){
        Chapter chapter = chapterMapper.selone(chapter_id);
        List<Chapter> list = chapterMapper.selnode(chapter.getChapterId());
        for (Chapter ct:list){
            Chapter cpe = selone(ct.getChapterId());
            chapter.getChildens().add(cpe);
        }
        return chapter;
    }

    /**
     * 单章节所属章节查询
     * */
    @Override
    public Chapter selonecha(Integer chapter_id) {
        return chapterMapper.selonecha(chapter_id);
    }

    @Override
    public Chapter selnodes(Integer chapter_id) {
        return chapterMapper.selnodes(chapter_id);
    }

    /**
     * 章节名查询
     * */
    @Override
    public Chapter selcnm(Integer chapter_id) {
        return chapterMapper.selcnm(chapter_id);
    }


    /**
     * 父级章节查询
     * */
    @Override
    public List<Chapter> selfath() {
        return chapterMapper.selfath();
    }

    /**
     * 更改章节
     * */
    @Override
    public int updtchapter(String chapter_name,Integer chapter_type, Integer chapter_id) {
        return chapterMapper.updtchapter(chapter_name,chapter_type,chapter_id);
    }
    /**
     * 删除章节(父级目录+子目录)
     * */
    @Override
    public int delchapter(Integer chapter_id) {
        return chapterMapper.delchapter(chapter_id);
    }

    /**
     * 删除子目录
     * */
    @Override
    public int delone(Integer chapter_id) {
        return chapterMapper.delone(chapter_id);
    }

    /**
     * 更改章节状态
     * */
    @Override
    public int updtchatype(Integer chapter_type, Integer chapter_id) {
        return chapterMapper.updtchatype(chapter_type,chapter_id);
    }
}
